﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Free2AskModel;
using Free2AskWeb.Models;
namespace Free2AskWeb.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private Free2AskDBEntities db;
        public HomeController()
        {
            db = new Free2AskDBEntities();
        }

        [Authorize]
        public ActionResult Index(int page=1)
        {
            Free2AskMVC4.Models.PageInfo pageInfo = new Free2AskMVC4.Models.PageInfo();
            pageInfo.TotalItems = db.Posts.OfType<Question>().Count();
            pageInfo.ItemsPerPage = 3;
            pageInfo.CurrentPage = page;

            //throw new Exception("Oh No, Oh No");
            QuestionsModel questions = new QuestionsModel(
                db.Posts.OfType<Question>()
                .OrderByDescending(q=>q.DatetimePosted)
                .Skip((page-1)*pageInfo.ItemsPerPage).Take(pageInfo.ItemsPerPage)//paging
                .ToList()
            );
            questions.Tags = db.Tags.ToList();            

            questions.PageInfo = pageInfo;

            return View(questions);            
        }

        

    }
}
